<?php

class ToolDB {

        protected static $ts_db = false;
        protected static $user_db = false;
        protected static $db = false;
        
        protected static $mycnf = array();
        
        protected static $server_id = null;
        
        public static function get( $lang = 'en', $wiki = 'wikipedia', $force_new = false ) {
        
                ProjectConfiguration::registerZend();
                
                if( is_object( self::$db ) && !$force_new ) return self::$db;
                
                if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
                        $timer = sfTimerManager::getTimer('Database (Zend)');
                }
                
                if( is_object( $lang ) && $lang instanceof sfWebRequest ) {
                        $wiki = $lang->getParameter( 'wiki', 'wikipedia' );
                        $lang = $lang->getParameter( 'lang', 'en' );
                }
    
                list( $user, $pass ) = self::parseIni();
                
                $ts_db = self::getTS();
                
                $select = $ts_db
                        ->select()
                        ->from( array( 'w' => 'wiki' ), array( 'w.dbname', 'w.server' ) )
                        ->where( 'domain = ?', "$lang.$wiki.org" )
                        ->query()
                        ->fetchAll();
                
                if( !count( $select ) ) {
                        throw new ToolDBException( sprintf( 'The wiki %s.%s.org does not exist', $lang, $wiki ) );
                }
                
                self::$server_id = $select[0]['server'];
                
                self::$db = Zend_Db::factory('Pdo_Mysql', array(
                        'host'     => 'sql-s' . $select[0]['server'] . '-rr',
                        'username' => $user,
                        'password' => $pass,
                        'dbname'   => $select[0]['dbname'],
                        'profiler' => sfConfig::get('sf_debug', false)
                ));
                
                if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
                        $timer->addTime();
                }
                
                return self::$db;
        }
        
        public static function getTS() {
                
                ProjectConfiguration::registerZend();
                
                if( is_object( self::$ts_db ) ) return self::$ts_db;
                
                if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
                        $timer = sfTimerManager::getTimer('Database (Zend)');
                }
                
                list( $user, $pass ) = self::parseIni();
                
                self::$ts_db = Zend_Db::factory('Pdo_Mysql', array(
                        'host'     => 'sql',
                        'username' => $user,
                        'password' => $pass,
                        'dbname'   => 'toolserver'
                ));
                
                if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
                        $timer->addTime();
                }
                
                return self::$ts_db;
        }
        
        public static function getUser() {
                
                ProjectConfiguration::registerZend();
                
                if( is_object( self::$user_db ) ) return self::$user_db;
                
                if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
                        $timer = sfTimerManager::getTimer('Database (Zend)');
                }
                
                list( $user, $pass ) = self::parseIni();
                
                self::$user_db = Zend_Db::factory('Pdo_Mysql', array(
                        'host'     => 'sql',
                        'username' => $user,
                        'password' => $pass,
                        'dbname'   => 'u_cyberpower678'
                ));
                
                if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) {
                        $timer->addTime();
                }
                
                return self::$user_db;
        }
        
        public static function parseIni() {
                
                if( count( self::$mycnf ) ) return self::$mycnf;
                
                $mycnf = parse_ini_file("/home/".get_current_user()."/.my.cnf");
                
                self::$mycnf = array( $mycnf['user'], $mycnf['password'] );
                
                return self::$mycnf;
        }
        
        
        public static function isOpen() {
                return is_object( self::$db );
        }
        
        public static function getServer() {
                return self::$server_id;
        }
}
